import bpy
from mathutils import Matrix
from bpy.types import Operator

import bpy.utils.previews
import os


g_项图标集合 = bpy.utils.previews.new()
g_当前预览 = ''




E_添加方式枚举 = [
	("添加", "添加", "", 0),
	("替换", "替换", "", 1),
]




class C_NODE_MENU_资源器_切换页面_R(bpy.types.Operator):
	bl_idname = 'object.resources_switch_page_r'
	bl_label = '切换页面'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		#bpy.ops.node.add_node(type='C_Node_集合取物体', use_transform=True)
		return {'FINISHED'}



class C_NODE_MENU_资源器_切换页面_L(bpy.types.Operator):
	bl_idname = 'object.resources_switch_page_l'
	bl_label = '切换页面'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		#bpy.ops.node.add_node(type='C_Node_集合取物体', use_transform=True)
		return {'FINISHED'}




class C_NODE_MENU_资源器_项(bpy.types.Operator):
	bl_idname = 'object.resources_item'
	bl_label = '项'
	bl_options = {'REGISTER', 'UNDO'}
	m_Icon = bpy.props.StringProperty(name="修改器名称", default='Solidify')
	def execute(operator, context):
		#bpy.ops.node.add_node(type='C_Node_集合取物体', use_transform=True)
		global g_当前预览
		g_当前预览 = operator.m_Icon
		print('g_当前预览 = ', g_当前预览)
		return {'FINISHED'}

	'''def invoke(self, context, event):
		#context.window_manager.invoke_search_popup(self)
		print('event invoke = ', event)
		return {'FINISHED'}
	def modal(self, context, event):
		print('event modal = ', event)
		return {'FINISHED'}
	def cancel(self, context):
		print('cancel = ')
	def draw(self, context):
		print('draw')
	def report(self, type, message):
		print(type, message)
	def check(self, context):
		print('check')
		return True'''

class C_PT_素材选取面板(bpy.types.Panel):
	bl_space_type = 'VIEW_3D'
	bl_region_type = 'UI'
	bl_label = "素材选取"

	
	def draw(self, context):
		#self.layout.prop(context.scene, 'm_添加类型')
		#return
		layout = self.layout.column()
		配置布局 = layout.row()
		
		配置布局.prop(context.scene, 'm_添加类型')
		配置布局.separator_spacer()
		配置布局.operator("object.resources_switch_page_r")
		配置布局.operator("object.resources_switch_page_l")
		
		
		layout = self.layout.column()
		global g_当前预览
		#if g_当前预览 != '':
		#	预览布局 = layout.row()
			#预览布局.template_icon(icon_value=g_项图标集合[g_当前预览].icon_id, scale=10.0)
			#预览布局.template_preview(id=g_项图标集合[g_当前预览])
			#预览布局.scale_x = 6
			#预览布局.scale_y = 6

		#layout = layout.box()
		#layout.alignment = 'CENTER'
		#layout.scale_x = 6
		#layout.scale_y = 6
		#layout.ui_units_x = 10
		#layout.ui_units_y = 10
		项布局 = layout.grid_flow(row_major=False, columns=0, even_columns=True, even_rows=True, align=True)
		#项布局 = layout.column_flow(columns=10)
		#项布局.scale_x = 6
		#项布局.scale_y = 6
		#self.layout.template_preview() , name='添加类型', expand=True, icon='DECORATE_LIBRARY_OVERRIDE'
		#print('g_项图标集合 = ', g_项图标集合)
		#for i in g_项图标集合:
		#	print('g_项图标集合 = ', g_项图标集合[i], i)
		#项布局.template_preview(g_项图标集合, show_buttons=True, parent=None, slot=None, preview_id="")
		for i in g_项图标集合:
			item = 项布局.operator("object.resources_item", icon_value=g_项图标集合[i].icon_id)
			项布局.alignment = 'CENTER'
			item.m_Icon = i
			#项布局.template_image_views(g_项图标集合[i].image_pixels)
			#项布局.template_preview(id=g_项图标集合[i])
			#项布局.template_icon(icon_value=g_项图标集合[i].icon_id, scale=1.0)
			#return
			#项布局.template_ID_preview(data=g_项图标集合, property='i')
			#项布局.template_any_ID(g_项图标集合, 'i')
			#项布局.prop(g_项图标集合, i)
			#项布局.template_icon_view(data=g_项图标集合, property=i)
			#项布局.template_track(g_项图标集合, i)







classes = (
	C_PT_素材选取面板,
	
	C_NODE_MENU_资源器_切换页面_L,
	C_NODE_MENU_资源器_切换页面_R,
	#add_mesh_bolt,
	C_NODE_MENU_资源器_项,

)

		
		
def register():
	from bpy.utils import register_class
	for cls in classes:
		register_class(cls)
	print('mbt--------------------------------mbt')
	bpy.types.Scene.m_添加类型 = bpy.props.EnumProperty(items=E_添加方式枚举, name="添加类型", default="添加")

	#测试添加项
	my_icons_dir = os.path.join(os.path.dirname("M:/Item/图标组/"), "01_icon")
	files = os.listdir(my_icons_dir)

	global g_项图标集合
	for f in files:
		#item = bpy.utils.previews.new()
		item = g_项图标集合.load(f, os.path.join(my_icons_dir, f), 'IMAGE')
		item.image_size = (100,100)
		#g_项图标集合[str(f)] = item

	# path to the folder where the icon is
	# the path is calculated relative to this py file inside the addon folder
	
	# load a preview thumbnail of a file and store in the previews collection
	# M:/Item/图标组/01_icon/0001.png
	


def unregister():
	from bpy.utils import unregister_class
	for cls in reversed(classes):
		unregister_class(cls)
	print('mbt--------------------------------mbt')




